package net.huashitong.supervision.task.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.TaskSub;
import net.huashitong.supervision.task.filter.TaskSubFilter;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/6
 * Time: 17:55
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Repository
public class TaskSubDao extends StringDao<TaskSub> {
    public List<TaskSub> queryFilter(TaskSubFilter taskSubFilter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。

        if (StringUtils.isNotBlank(taskSubFilter.getSysId())) {
            query.append(" and sysId = :sysId").setParam("sysId", taskSubFilter.getSysId());
        }
        if (StringUtils.isNotBlank(taskSubFilter.getTaskMainId())) {
            query.append(" and taskMainId = :taskMainId").setParam("taskMainId", taskSubFilter.getTaskMainId());
        }
        if (StringUtils.isNotBlank(taskSubFilter.getSubName())) {
            query.append(" and subName like :subName").setParam("subName", StringUtils.getLikewords(taskSubFilter.getSubName()));
        }
        //设置排序
        if (ObjectUtils.isNotBlank(taskSubFilter.getPaging())) {
            if (StringUtils.isBlank(taskSubFilter.getPaging().getSortCol())) {
                taskSubFilter.getPaging().setSortCol("sysId");
                taskSubFilter.getPaging().setSortVal("desc");
            }
        }
        return query.listResult(taskSubFilter);
    }

    public List<TaskSub> queryFilterNoPage(TaskSubFilter taskSubFilter) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。

        if (StringUtils.isNotBlank(taskSubFilter.getSysId())) {
            query.append(" and sysId = :sysId").setParam("sysId", taskSubFilter.getSysId());
        }
        if (StringUtils.isNotBlank(taskSubFilter.getTaskMainId())) {
            query.append(" and taskMainId = :taskMainId").setParam("taskMainId", taskSubFilter.getTaskMainId());
        }
        if (StringUtils.isNotBlank(taskSubFilter.getSubName())) {
            query.append(" and subName like :subName").setParam("subName", StringUtils.getLikewords(taskSubFilter.getSubName()));
        }
        //设置排序
        if (ObjectUtils.isNotBlank(taskSubFilter.getPaging())) {
            if (StringUtils.isBlank(taskSubFilter.getPaging().getSortCol())) {
                taskSubFilter.getPaging().setSortCol("sysId");
                taskSubFilter.getPaging().setSortVal("desc");
            }
        }
        return query.listResult();
    }

    public void batchDeleteTaskIds(List<String> ids) {
        StringBuffer hql = (new StringBuffer("delete from TaskSub")).append(" obj where obj.taskMainId in(:taskMainId)");
        Query query = this.getSession().createQuery(hql.toString());
        query.setParameterList("taskMainId", ids);
        query.executeUpdate();
    }

    /**
     * 根据主任务id查询子任务
     * @param id
     * @return
     */
    public List<TaskSub> getByMainId(String id) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null");//只查询删除标识为空的记录。
        query.append(" and taskMainId = :taskMainId").setParam("taskMainId", id);
        return query.listResult();
    }
    public List<TaskSub> getByMainIdDel(String id) {
        GenericQuery query = createQuery("obj");
        query.append(" and del = :del").setParam("del","0" );//只查询删除的记录。
        query.append(" and taskMainId = :taskMainId").setParam("taskMainId", id);
        return query.listResult();
    }
}
